<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateAdminTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $connection = config('admin.database.connection') ?: config('database.default');

        Schema::connection($connection)->create(config('admin.database.users_table'), function (Blueprint $table) {
            $table->increments('id');
            $table->string('genre', 16)->default('')->comment('用户类型 1:超级用户 2:店铺管理员 3:普通用户 4:');
            $table->string('username', 16)->default('')->comment('用户名');
            $table->string('password', 64)->default('')->comment('密码');
			$table->string('avatar', 32)->default('')->comment('头像');
			$table->string('name', 16)->default('')->comment('姓名');
			$table->string('mobile', 16)->default('')->comment('手机');
			$table->string('wechat', 32)->default('')->comment('微信号');
			$table->date('birthday')->nullable()->comment('生日');
			$table->boolean('gender')->default(0)->comment('性别 1:男 2:女');
            $table->string('remember_token', 100)->default('');
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.roles_table'), function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('')->comment('逻辑名称');
            $table->string('slug', 50)->default('')->comment('角色名称');
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.permissions_table'), function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('')->comment('逻辑名称');
            $table->string('slug', 50)->default('')->comment('显示名称');
            $table->string('http_method')->default('')->comment('请求方法');
            $table->text('http_path')->default('')->comment('请求路由');
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.menu_table'), function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedTinyInteger('parent_id')->default(0);
            $table->unsignedTinyInteger('rank')->default(0)->comment('排序');
            $table->string('title', 16)->default('')->comment('名称');
            $table->string('icon', 32)->default('')->comment('图标');
            $table->string('uri', 50)->default('');
            $table->string('permission')->default('');

            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.role_users_table'), function (Blueprint $table) {
            $table->integer('role_id');
            $table->integer('user_id');
            $table->index(['role_id', 'user_id']);
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.role_permissions_table'), function (Blueprint $table) {
            $table->integer('role_id');
            $table->integer('permission_id');
            $table->index(['role_id', 'permission_id']);
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.user_permissions_table'), function (Blueprint $table) {
            $table->integer('user_id');
            $table->integer('permission_id');
            $table->index(['user_id', 'permission_id']);
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.role_menu_table'), function (Blueprint $table) {
            $table->integer('role_id');
            $table->integer('menu_id');
            $table->index(['role_id', 'menu_id']);
            $table->timestamps();
        });

        Schema::connection($connection)->create(config('admin.database.operation_log_table'), function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedMediumInteger('user_id')->default(0)->comment('用户ID');
            $table->string('path')->default('')->comment('路径');
            $table->string('method', 10)->default('')->comment('方法');
            $table->string('ip', 16)->default('');
            $table->text('input');
            $table->index('user_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $connection = config('admin.database.connection') ?: config('database.default');

        Schema::connection($connection)->dropIfExists(config('admin.database.users_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.roles_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.permissions_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.menu_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.user_permissions_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.role_users_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.role_permissions_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.role_menu_table'));
        Schema::connection($connection)->dropIfExists(config('admin.database.operation_log_table'));
    }
}
